<?php
/*
 * Created on Apr 3, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
class Utils_Image
{
   var $gd_im=NULL;          // a gd image oject
   var $mime_type;
   var $file_type;
   var $error_msg;

   function Utils_Image($file_name='')
   {
      $data=$this->get_image($file_name);
      $this->gd_im=NULL;
      $this->file_type='';
      if ($data['gd_im']==NULL)
      {
         $this->error_msg = $data['error_msg'];
         return NULL;
      }
      $this->gd_im=$data['gd_im'];
      $this->mime_type=$data['mime_type'];
      $this->file_type=$data['file_type'];
      $this->gd_im = $data['gd_im'];
      $this->width = $data['width'];
      $this->height = $data['height'];
      return $this->gd_im;
   }

   function destroy()
   {
      return imageDestroy($this->gd_im);
   }

   function create_resize($out_file,$wm_file='', $t_wd = 120,$t_ht = 90, $pos_x = 0, $pos_h = 0)
   {
      if ($this->gd_im!=NULL)
      {
         $o_wd = imagesx($this->gd_im) ;
         $o_ht = imagesy($this->gd_im) ;

         if($o_wd/$o_ht>$t_wd/$t_ht)
         {// thumbnail width = target * original width / original height
            $t_ht = round($o_ht * $t_wd / $o_wd) ;
            $t_im = imageCreateTrueColor($t_wd,$t_ht);
         }
         else
         {// thumbnail height = target * original height / original witdh
            $t_wd = round($o_wd * $t_ht / $o_ht) ;
            $t_im = imageCreateTrueColor($t_wd,$t_ht);
         }
    
         imageCopyResampled($t_im, $this->gd_im, 0, 0, 0, 0, $t_wd, $t_ht, $o_wd, $o_ht);

         if (!empty($wm_file))
         {
            //Create water mark
            $wm_t_im=$this->draw_watermark($wm_file,$t_im, $pos_x,$pos_h);
            if ($wm_t_im)
            {
               $t_im=$wm_t_im;
            }
         //imagefttext  ( $t_im  , 40  , 90  , 400  , 400  , 100  , "./tahoma.ttf"  , "abc THS IS SO FUCKING COOL" );
         }
         imageJPEG($t_im,$out_file,75);
         imageDestroy($t_im);
         return true;
      }
      else
      {
         return false;
      }
   }

   function create_resize_w($out_file,$wm_file='', $t_wd = 120)
   {
      if ($this->gd_im!=NULL)
      {
         $o_wd = imagesx($this->gd_im) ;
         $o_ht = imagesy($this->gd_im) ;

         if($o_wd>$t_wd)
         {
            $t_ht = round($o_ht * $t_wd / $o_wd) ;
            $t_im = imageCreateTrueColor($t_wd,$t_ht);
         }
         else
         {// thumbnail height = target * original height / original witdh
            $t_wd = $o_wd  ;
			$t_ht = $o_ht  ;
            $t_im = imageCreateTrueColor($t_wd,$t_ht);
         }
         imageCopyResampled($t_im, $this->gd_im, 0, 0, 0, 0, $t_wd, $t_ht, $o_wd, $o_ht);

         if (!empty($wm_file))
         {
            //Create water mark
            $wm_t_im = $this->draw_watermark($wm_file,$t_im);
            if ($wm_t_im)
            {
               $t_im = $wm_t_im;
            }
//            imagefttext  ( $t_im  , 40  , 90  , 400  , 400  , 555555  , "./tahoma.ttf"  , "abc THS IS SO FUCKING COOL" );
            
         }
         imageJPEG($t_im,$out_file,75);
         imageDestroy($t_im);
         return true;
      }
      else
      {
         return false;
      }
   }


//   function draw_watermark($image_file,$img)
   function draw_watermark($image_file,$img, $pos_x, $pos_h)
   {
       $data=$this->get_image($image_file);
       $this->watermark=$data;
       if (NULL==$data['gd_im'])
       {
          return false;
       }
       $wm_img=$data['gd_im'];
       $wm_w=imagesx($wm_img);
       $wm_h=imagesy($wm_img);
       $img_w=imagesx($img);
       $img_h=imagesy($img);
       // calculate the position (bottom-right)
       if($pos_x == null && $pos_h == null)
       {
	       $pos_x=$img_w-$wm_w-1; // -1 for margin
	       $pos_h=$img_h-$wm_h-1;
       }
       
       // draw the water mark
       imagecopymerge($img, $wm_img, $pos_x, $pos_h, 0, 0, $wm_w, $wm_h, 10); // 20% transparent
//       imagecopyresampled( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h );
   	   imagecopyresampled( $img, $wm_img, $pos_x, $pos_h, 0, 0, $wm_w, $wm_h,$wm_w, $wm_h);
//   	   imagecopyresampled( $img, $wm_img, $pos_x, $pos_h, 0, 0, $wm_w, $wm_h,$wm_w, $wm_h);

   
       return $img;
   }

   function create_avatar($out_file, $t_wd = 100,$t_ht = 100)
   {
      if ($this->gd_im!=NULL)
      {
         //echo "<br>debug:",$t_wd,':',$t_ht;
         $o_wd = $this->width ;
         $o_ht = $this->height ;

         if($o_wd/$o_ht>$t_wd/$t_ht)
         {// copy width = copy height * avatar width / avatar height
            $copy_ht=$o_ht;
            $copy_wd = round($copy_ht * $t_wd / $t_ht) ;
            $source_x=round(($o_wd-$copy_wd)/2);
            $source_y=0;
         }
         else
         {// copy height= copy width * avatar height / avatar width
            $copy_wd=$o_wd;
            $copy_ht = round($copy_ht * $t_ht / $t_wd) ;
            $source_x=0;
            $source_y=round(($o_ht-$copy_ht)/2);;
         }
         //echo " s: $source_x, $source_y, o: $o_wd, $o_ht, c: $copy_wd, $copy_ht";
         $t_im = imageCreateTrueColor($t_wd,$t_ht);
         imageCopyResampled($t_im, $this->gd_im, 0, 0, $source_x, $source_y,
                                   $t_wd, $t_ht, $copy_wd, $copy_ht);
         imageJPEG($t_im,$out_file,75);
         imageDestroy($t_im);
         return true;
      }
      else
      {
         return false;
      }
  }

  function get_image($file_name='')
  {
      $data=array();
      $data['file_type']='';
      $data['gd_img']=NULL;
      if (empty($file_name))
      {
         $data['error_msg']="Empty filename";
         return $data;
      }
      $image_info = getImageSize($file_name) ; // see EXIF for faster way
     
      $data['mime_type']=$image_info['mime'];

      switch ($image_info['mime'])
      {
          case 'image/gif':
              $data['file_type']='gif';
              if (imagetypes() & IMG_GIF)  { // not the same as IMAGETYPE
                  $data['gd_im'] = imageCreateFromGIF($file_name) ;
              } else {
                  $data['error_msg'] = sprintf("%s files are not supported",'GIF');
              }
              break;
          case 'image/jpeg':
              $data['file_type']='jpg';
              if (imagetypes() & IMG_JPG)  {
                  $data['gd_im'] = imageCreateFromJPEG($file_name) ;
              } else {
                  $data['error_msg'] = sprintf('%s files are not supported','JPG');
              }
              break;
          case 'image/png':
              $data['file_type']='png';
              if (imagetypes() & IMG_PNG)  {
                  $data['gd_im'] = imageCreateFromPNG($file_name) ;
              } else {
                  $data['error_msg'] = sprintf('%s files are not supported','PNG');
              }
              break;
          case 'image/wbmp':
              $data['file_type']='bmp';
              if (imagetypes() & IMG_WBMP)  {
                  $data['gd_im'] = imageCreateFromWBMP($file_name) ;
              } else {
                  $data['error_msg'] = sprintf('%s files are not supported','WBMP');
              }
              break;
          default:
              $data['error_msg'] = sprintf('%s files are not supported',$image_info['mime']);
              return $data;
              break;
      }

      $data['width'] = imagesx($data['gd_im']) ;
      $data['height'] = imagesy($data['gd_im']) ;
      return $data;
  }
  
  //ANH:
  
  public function emboss($pic1, $patt) {

      $source = imagecreatefromjpeg($pic1); // Source
      $pattern = imagecreatefromjpeg($patt); // pattern

      list ($width, $height) = getimagesize($pic1);
      list ($widthpatt, $heightpatt) = getimagesize($patt);

      $new_width = $width;
      $new_height = $width / $widthpatt * $heightpatt;

      if ($new_height > $height) {
    $offset = intval(($new_height - $height) / 2);
      }elseif ($new_height < $height) {
    $offset = intval(($new_height - $height) / 2);
       } else {
       $offset = 0;
        }

    $image_p = imagecreatetruecolor($new_width, $new_height);
    $pattern = imagecreatefromjpeg($patt);
     imagecopyresampled($image_p, $pattern, 0, 0, 0, 0, $new_width, $new_height, $widthpatt, $heightpatt);

    $im = imagecreatetruecolor($width, $height);
        for ($y = 0; $y < $height; $y ++) {
       for ($x = 0; $x < $width; $x ++) {
         $colors = imagecolorsforindex($source, imagecolorat($source, $x, $y));
         $pattern_color = imagecolorsforindex($image_p, imagecolorat($image_p, $x, ($y + $offset)));
        
             //changes brightness depending on luminance
        if (($y + $offset +1) > 0 && ($y + $offset) < ($new_height -1)) {
          $brightness = abs(($pattern_color['red'] * 50 / 255) - 50);
           if ($pattern_color['red'] < 150) {
             $change = true;
           } else {
              $change = false;
              $tally = 0;
           }

           if ($change && $tally < 2) {
             $highlight = 1.8;
             $tally ++;
           } else {
             $highlight = 1;
           }
           $brightness = $brightness * $highlight;
        } else {
            $brightness = 0;
        }
        $r = (($colors['red'] + $brightness) > 255) ? 255 : ($colors['red'] + $brightness);
        $g = (($colors['green'] + $brightness) > 255) ? 255 : ($colors['green'] + $brightness);
        $b = (($colors['blue'] + $brightness) > 255) ? 255 : ($colors['blue'] + $brightness);
        $test = imagecolorallocate($im, $r, $g, $b);
        imagesetpixel($im, $x, $y, $test);
       }
        }

    imagepng($im);
    imagedestroy($im);
   }
}

?>
